var _table =null;
$(document).ready(function(){
    initSysUserRoleTable();
});


function savaOrUpdateUserRole(){
	
	var chk_value =[]; 
	$('#table-user input[type="checkbox"]:checked').each(function(){ 
		chk_value.push($(this).val()); 
	}); 
	
	if(chk_value.length == 0){
		layer.alert('请先选中角色信息!');
		return false;
	}
	
	var json_str = chk_value.join(",");
	ajaxRequestJson("savaOrUpdateUserRole",{
		"idStr":json_str,
		"uid":$("#uid").val()
	},function(sh_result){
		if(sh_result>0){
			show_alert('保存成功',function(){layer_close()});
		}
	});
}




function refulsh(){
	userManage.fuzzySearch = false;
    _table.draw();
}



function initSysUserRoleTable(){
	  var $table = $('#table-user');
	    _table = $table.dataTable($.extend(true,{},CONSTANT.DATA_TABLES.DEFAULT_OPTION_NOPAG, {
		 orderMulti: false,
		 bFilter: false,
		 order: [],
		 renderer: "bootstrap",
		 pagingType: "simple_numbers",
		 aaSorting: [[ 1, "desc" ]],
		 paging: false,
		 bStateSave: true,
		 language:"",
			pageLength:100,
			lengthMenu:[10,20,50,100],
			iDisplayLength: 10,
	        ajax : function(adata, callback, settings) {
	           var param = "";
	            $.ajax({
	                    type: "post",
	                    url: "querySysUserRolePage",
	                    cache : false,
	                    data: param,
	                    dataType: "json",
	                    success: function(reslut) {
                            var returnData = {};
                            returnData.draw = adata.draw;
                            returnData.data = reslut.data;
                            callback(returnData);
	                    },
	                    error: function(XMLHttpRequest, textStatus, errorThrown) {
	                       console.log("查询失败");
	                    }
	                });
	        },
    "columns": [
				 {"data":  function ( row, type, set ) {
	             return  '<input type="checkbox" class="iCheck" value='+row.id+'>';        
	         },"bSortable":false},
				{
					 "data": "roleName","bSortable": false
				},
				{
					 "data": "roleDescription","bSortable": false
				},
     ],
     "columnDefs": [

//         {
//             "targets": [20], // 目标列位置，下标从0开始
//             "data": "id", // 数据列名
//             "render": function(data, type, full) { // 返回自定义内容
//            	 var str ="";
//            	 if("1"==full.status){
//            		 str ='<a style="text-decoration:none" onClick="data_stop(this,'+data+')" href="javascript:void(0);" title="禁用">';
//            	 }else{
//            		 str ='<a style="text-decoration:none" onClick="data_start(this,'+data+')" href="javascript:void(0);" title="启用">';
//            	 }
//            	 
//            	 str =str+'<i class="Hui-iconfont">&#xe615;</i></a> <a title="编辑" href="javascript:void(0);" onclick="data_edit(\'管理员编辑\',\'user-add?id='+data+'\',\'700\',\'700\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:void(0);" onclick="data_del(this,'+data+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
//            	 return str;
//                
//             }
//         }
     ],
     "initComplete": function (settings, json) {
    	 var uid = $("#uid").val();
    	 if(uid != null){
    		 ajaxRequestJson("getRoleByUid",{
    			 "uid":uid
    		 },function(ret){
    			 if(ret != null && ret.length>0){
    				 for(var i=0;i<ret.length;i++){
    					 $('input:checkbox[value='+ret[i].rid+']').attr("checked", true);
    				 }
    			 }
    		 })
    	 }
		 
	},
	 })).api();   

};  
